package cc.sheji.controller;


import cc.sheji.Service.JobService;
import cc.sheji.domain.Job;
import cc.sheji.domain.JobQueryCriteria;
import cc.sheji.domain.JsonResult;
import cc.sheji.domain.PayInfo;
import cc.sheji.util.HttpUtil;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;


@RestController
@RequestMapping(value = "/api/v1/job")
public class JobController {

    @Autowired
    JobService jobService;
    @Autowired
    HttpUtil httpUtil;
    @PostMapping
    public JsonResult create(@RequestBody Job job) throws Exception {
        job.setId(null);
        return JsonResult.Success(jobService.create(job));
    }

    @PostMapping("/sameProduct")
    public JsonResult sameProduct(@RequestBody Job job) throws Exception {
        job.setId(null);
        return JsonResult.Success(jobService.sameProduct(job));
    }

    @GetMapping(value = "/{id}")
    public JsonResult get(@PathVariable("id") @NotNull Long id) {

        return JsonResult.Success(jobService.get(id));
    }

    @GetMapping(value = "/payInfo")
    public JsonResult payInfo(
            PayInfo payInfo) {

        return JsonResult.Success(jobService.payInfo(payInfo));
    }

    @PutMapping(value = "/{id}")
    public JsonResult update(@PathVariable("id") @NotNull Long id, @RequestBody Job job) {

        job.setId(id);
        return JsonResult.Success(jobService.update(job));
    }


    @GetMapping(value = "/mine")
    public JsonResult mine(JobQueryCriteria criteria, Pageable pageable) {

        criteria.setUserId(StpUtil.getLoginIdAsLong());
        return JsonResult.Success(jobService.list(criteria, pageable));
    }

    @GetMapping()
    public JsonResult list(JobQueryCriteria criteria, Pageable pageable) {

        return JsonResult.Success(jobService.list(criteria, pageable));
    }

}
